// webview.js
Page({
  data: {
    webviewUrl: '',
    title: '加载中...'
  },

  onLoad(options) {
    const { url, title } = options
    console.log('url:', url)
    if (url) {
      this.setData({
        webviewUrl: decodeURIComponent(url),
        title: title ? decodeURIComponent(title) : '网页浏览'
      })
      console.log('webviewUrl:', this.data.webviewUrl)
    } else {
      // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
      wx.showToast({
        title: '网页地址无效',
        icon: 'none'
      })
      setTimeout(() => {
        // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateBack.html
        wx.navigateBack()
      }, 1500)
    }
  },

  // 返回按钮
  goBack() {
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/route/wx.navigateBack.html
    wx.navigateBack()
  },

  // webview加载完成
  onWebviewLoad() {
    console.log('webview加载完成')
  },

  // webview加载错误
  onError(e) {
    console.error('webview加载错误:', e)
    // 官方文档: https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showToast.html
    wx.showToast({
      title: '网页加载失败',
      icon: 'none'
    })
  },

  // 接收webview消息
  onMessage(e) {
    console.log('收到webview消息:', e.detail.data)
  },

  // 分享功能
  onShareAppMessage() {
    return {
      title: this.data.title,
      path: `/pages/webview/webview?url=${encodeURIComponent(this.data.webviewUrl)}&title=${encodeURIComponent(this.data.title)}`
    }
  }
})